﻿using System.Windows.Media;
using System.Windows;

namespace DynamicGeometry
{
    public partial class CartesianGrid : CompositeFigure
    {
        public CartesianGrid()
        {
            ShapeStyle arrowStyle = new ShapeStyle()
            {
                Color = Color.FromArgb(255, 128, 128, 255),
                Fill = Color.FromArgb(255, 128, 128, 255),
                StrokeWidth = 1,
                Name = "ArrowStyle"
            };
            LineStyle axisStyle = new LineStyle()
            {
                Color = Color.FromArgb(255, 128, 128, 255),
                Name = "AxisStyle",
                StrokeWidth = 1
            };
            LineStyle gridStyle = new LineStyle()
            {
                Color = Colors.LightGray,
                Name = "GridStyle",
                StrokeWidth = 0.5
            };
            TextStyle labelsStyle = new TextStyle()
            {
                Color = Color.FromArgb(255, 128, 128, 255),
                FontSize = 12.0,
                Name = "LabelsStyle"
            };

            OriginPoint = Factory.CreatePointByCoordinates(Drawing, () => 0, () => 0);
            XUnitPoint = Factory.CreatePointByCoordinates(Drawing, () => 1, () => 0);
            YUnitPoint = Factory.CreatePointByCoordinates(Drawing, () => 0, () => 1);
            OriginPoint.Name = "Origin";
            XUnitPoint.Name = "XUnitPoint";
            YUnitPoint.Name = "YUnitPoint";
            OriginPoint.Visible = false;
            XUnitPoint.Visible = false;
            YUnitPoint.Visible = false;
            XAxisLine = Factory.CreateAxis(Drawing, new FigureList() { OriginPoint, XUnitPoint });
            YAxisLine = Factory.CreateAxis(Drawing, new FigureList() { OriginPoint, YUnitPoint });

            AxisLabels = new AxisLabelsCollection() { Drawing = Drawing };
            GridLines = new RectangularGridLinesCollection() { Drawing = Drawing };

            XAxisLine.Arrow.Style = arrowStyle;
            XAxisLine.Line.Style = axisStyle;
            YAxisLine.Arrow.Style = arrowStyle;
            YAxisLine.Line.Style = axisStyle;
            GridLines.Style = gridStyle;
            AxisLabels.Style = labelsStyle;

            Add(
                OriginPoint,
                //XUnitPoint,
                //YUnitPoint,
                //XAxisLine,
                //YAxisLine,
                //AxisLabels,
                GridLines
                );
        }

        public PointByCoordinates OriginPoint { get; set; }
        public PointByCoordinates XUnitPoint { get; set; }
        public PointByCoordinates YUnitPoint { get; set; }
        public Axis XAxisLine { get; set; }
        public Axis YAxisLine { get; set; }
        AxisLabelsCollection AxisLabels { get; set; }
        GridLinesCollection GridLines { get; set; }

        public override bool Visible
        {
            get
            {
                return base.Visible;
            }
            set
            {
                base.Visible = value;
                AxisLabels.Visible = value;
                GridLines.Visible = value;
                XAxisLine.Visible = value;
                YAxisLine.Visible = value;
            }
        }

        public static Brush AxesBrush = new SolidColorBrush(Axis.Color);

        public static FrameworkElement GetIcon()
        {
            var builder = IconBuilder
                .BuildIcon()
                .Polygon(
                    new SolidColorBrush(Colors.Blue),
                    new SolidColorBrush(Colors.Blue),
                    new Point(0.5, 0),
                    new Point(0.4, 0.2),
                    new Point(0.6, 0.2))
                .Polygon(
                    new SolidColorBrush(Colors.Blue),
                    new SolidColorBrush(Colors.Blue),
                    new Point(1, 0.5),
                    new Point(0.8, 0.4),
                    new Point(0.8, 0.6))
                .Line(Color.FromArgb(255, 0, 0, 255), 0.5, 0, 0.5, 1)
                .Line(Color.FromArgb(255, 0, 0, 255), 0, 0.5, 1, 0.5);
            for (double i = 0.1; i < 1; i += 0.2)
            {
                builder.Line(Color.FromArgb(100, 0, 0, 255), i, 0, i, 1);
                builder.Line(Color.FromArgb(100, 0, 0, 255), 0, i, 1, i);
            }
            return builder.Canvas;
        }
    }
}
